package com.cqvip.innocence.project.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqvip.innocence.framework.jwt.dto.LoginAdmin;
import com.cqvip.innocence.framework.jwt.service.AdminTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @ClassName AbstractController
 * @Description Controller抽象
 * @Author Innocence
 * @Date 2020/9/22 11:19
 * @Version 1.0
 */
public abstract class AbstractController {


    @Autowired
    private AdminTokenService adminTokenService;


    /**
     * 获取当前登录的管理员
     *
     * @return com.cqvip.innocence.framework.jwt.dto.LoginAdmin
     * @author Innocence
     * @date 2022/7/21
     */
    protected LoginAdmin getCurrentAdmin() {
        String token = adminTokenService.getTokenFromRequest(getRequest());
        return adminTokenService.getLoginAdmin(token);
    }

    public final Logger log = LoggerFactory.getLogger(this.getClass());


    public static final String PARAMS_NOT_NULL = "参数不能为null";


    private static final Long DEFAULT_PAGE_NUM = 1L;
    private static final Long DEFAULT_PAGE_SIZE = 10L;
    private static final String PAGE_NUM_KEY = "pageNum";
    private static final String PAGE_SIZE_KEY = "pageSize";

    //mybatis-plus的分页参数
    private ThreadLocal<Page> pageParams = new ThreadLocal();

    private ThreadLocal<Boolean> initedParamsVariable = new ThreadLocal();
    private ThreadLocal<HttpServletRequest> request = new ThreadLocal();
    private ThreadLocal<HttpServletResponse> response = new ThreadLocal();

    //elasticsearch的分页参数
    private ThreadLocal<PageRequest> pageRequest = new ThreadLocal<>();

    protected void initParamsVariable() {
        Long page;
        Long size;
        String param = this.getParam(PAGE_NUM_KEY);
        if (StrUtil.isNotBlank(param)) {
            page = Long.valueOf(param);
        } else {
            page = DEFAULT_PAGE_NUM;
        }
        String sizeStr = this.getParam(PAGE_SIZE_KEY);
        if (StrUtil.isNotBlank(sizeStr)) {
            size = Long.valueOf(sizeStr);
        } else {
            size = DEFAULT_PAGE_SIZE;
        }
        this.pageParams.set(new Page(page, size));
    }

    /**
     * 组装es的分页参数
     * @author Innocence
     * @date 2022/8/16
     * @return void
     */
    protected void initElasticSearchPageParam(){

        Integer current;
        Integer size;
        String param = this.getParam(PAGE_NUM_KEY);
        if (StrUtil.isNotBlank(param)) {
            current = Integer.valueOf(param) - 1;
        } else {
            current = 0;
        }
        String sizeStr = this.getParam(PAGE_SIZE_KEY);
        if (StrUtil.isNotBlank(sizeStr)) {
            size = Integer.valueOf(sizeStr);
        } else {
            size = 20;
        }
        this.pageRequest.set(PageRequest.of(current, size));
    }

    protected String getParam(String key) {
        return this.getRequest().getParameter(key);
    }

    protected HttpServletRequest getRequest() {
        HttpServletRequest request = this.request.get();
        if (request == null) {
            RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            request = ((ServletRequestAttributes) requestAttributes).getRequest();
        }
        return request;
    }

    protected HttpServletResponse getResponse() {
        HttpServletResponse response = this.response.get();
        if (response == null) {
            RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
            HttpServletResponse servletResponse = ((ServletRequestAttributes) attributes).getResponse();
            response = servletResponse;
        }
        return response;
    }

    /**
     * 获取mybatis-plus分页参数
     *
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page
     * @author Innocence
     * @date 2022/5/11
     */
    protected Page getPageParams() {
        this.initParamsVariable();
        return this.pageParams.get();
    }

    /**
     * 获取elasticsearch的分页参数
     * @author Innocence
     * @date 2022/8/16
     * @return org.springframework.data.domain.PageRequest
     */
    protected PageRequest getPageRequest(){
        this.initElasticSearchPageParam();
        return this.pageRequest.get();
    }

}
